--
-- Author: mm
-- Date: 2018-09-22 10:59:30
--
-- CorePropController
--

local fileName = string.getModeName(...)
local CorePropController = class(fileName, app.mvc.ControllerBase)

local CorePropUIHelp = require("app.coreProp.CorePropUIHelp")

function CorePropController:ctor(data)
    self.super.ctor(self, data)
end

function CorePropController:init()
    self._model = app:getInst("CorePropModel")

    self._bagHandler = UD.bag:onChanged(handler(self, self.updateBag))
    self._redPointHandler = UD.redPoint:onChanged(handler(self, self.updateRedPoint))
end

function CorePropController:removeListeners()
    self.super.removeListeners(self)
    UD.bag:unChanged(self._bagHandler)
    UD.redPoint:unChanged(self._redPointHandler)
end

function CorePropController:updateBag(uid, item, amountGot, opt)
    -- 核心属性 道具发生变化
    if item and item:getId() == Constant.CoreProp_ItemId then
        UD.redPoint:checkStateByKey(Constant.FUNC_TYPE_CORE_PROP, true)
    end
end

function CorePropController:updateRedPoint(changeKeys)
    if changeKeys[RedPointConstant.CorePorp_Add] ~= nil then
        if cc.isAlive(self._view) then
            self._view:refreshRedPoint()
        end
    end
end

function CorePropController:onEvent(eventName, data)
end

function CorePropController:listEvent()
    return {}
end

function CorePropController:openView(data)
    if cc.isDead(self._view) then
        local CorepropView = require("app.coreProp.views.CorePropView")
        self._view = CorepropView.new(app.mvc.VIEWTYPE_BLUR)
        self._view:onCloseView(handler(self, self.closeView))
        self._view:onClearView(handler(self, self.clearView))
        self._view:onOperate(handler(self, self.onOperate))
    end
    self:addChild(self._view, data)
    self:refreshView(true)
    self._view:show()
    self._view:checkGuide()
end


function CorePropController:closeView(data)
    if self._view then
        self:removeChild(self._view, data)
        self:clearView()
    end
end

function CorePropController:clearView()
    if self._view then
        self._view = nil
    end
end


function CorePropController:refreshView(notFresh)
    if not cc.isDead(self._view) then
        self._view:setViewData(self._model:getViewData())
    end

    if not cc.isDead(self._exchangePointView) then
        self._exchangePointView:setViewData(self._model:getViewData())
    end
end
-- 
function CorePropController:openExchangePointView()
    local CorePropExchangePointView = require("app.coreProp.views.CorePropExchangePointView")
    local view = CorePropExchangePointView.new()
    view:onCloseView(function(sender)
        self._exchangePointView = nil
    	UIHandler:removePopupUiByKey(sender:getViewName())
    end)
    view:onOperate(handler(self, self.onOperate))
    UIHandler:addPopupUi(view, view:getViewName())
    self._exchangePointView = view
    self:refreshView(true)
    view:show()
    view:checkGuide()
end

-------------------------------------------------------

-------------------------------------------------------
function CorePropController:onOperate(op, data)
    -- print("CorePropController:onOperate op = " .. op, data)
    if op == "addLevel" then
        self._model:reqAddOneCoreProp(data, 1)
    elseif op == "exchangePoint" then
        self._model:reqGainCorePropPoint(data)
    elseif op == "openExchangePointView" then
        self:openExchangePointView()
    end
end

return CorePropController

